package pluginUtils;

public class Matrix4D {
	protected double[][] coef=new double[4][4]; 
	
	public void set(int l,int c,double v){
		coef[l][c]=v; 
	}
	
	public Matrix4D(){
	
	}
	
	public Matrix4D(double[][] val){
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++)
				this.coef[i][j]=val[i][j]; 
	}
	
	public static Matrix4D makeId(){
		Matrix4D ID=new Matrix4D(); 
		for(int i=0;i<4;i++) ID.coef[i][i]=1; 
		return ID; 
	}
	
	static final Matrix4D ID=makeId(); 
	
	public static Matrix4D mul(Matrix4D A,Matrix4D B){
		Matrix4D resu=new Matrix4D(); 
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++){
				resu.coef[i][j]=0.0;
				for(int k=0;k<4;k++)
					resu.coef[i][j]+=A.coef[i][k]*B.coef[k][j]; 
			}
		return resu; 
	}
	
	public Vector4D transfo(Vector4D src){
		Vector4D dest=new Vector4D();
		for(int i=0;i<4;i++) // calcul de dest.coord[i]
		{
			dest.coord[i]=0; 
			for(int j=0;j<4;j++) dest.coord[i]+=coef[i][j]*src.coord[j]; 
		}
		return dest; 
	}
	
	public String toString(){
		String s="";
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++)
				s+=" "+coef[i][j]; 
			if(i<3)s+="\n"; 
		}
		return s; 
	}
	
	public double determinant(){ 
		double resu=0;
		int ind[][]={{1,2,3},{0,2,3},{0,1,3},{0,1,2}}; 
		// premiere colonne
		
		int signe; 
		for(int j=0;j<4;j++){
			System.out.println("test "+j); 
			if(j%2!=0) signe=-1; else signe=1; 
			// Construire la matrice 3D
			double[][] m3d=new double[3][3];
			for(int l=0;l<3;l++)
			for(int i=0;i<3;i++) 
				m3d[l][i]=this.coef[ind[j][l]][i+1];
	
			resu+=det3D(m3d)*this.coef[j][0]*signe;
		
		}// j
		return resu;
	}
	
	private static double det3D(double[][] A){
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++) System.out.print(A[i][j]+" "); 
			System.out.println(); 
		}
		double resu=0; 
		resu=A[0][0]*(A[1][1]*A[2][2]-A[1][2]*A[2][1]); 
		System.out.println("1 "+resu); 
		resu+=-A[1][0]*(A[0][1]*A[2][2]-A[0][2]*A[2][1]);
		System.out.println("2 "+resu); 
		resu+=A[2][0]*(A[0][1]*A[1][2]-A[1][1]*A[0][2]); 
		System.out.println("3 "+resu); 
		System.out.println(resu); 
		return resu; 
	}
	

	public static void main(String[] args) {
	
		double init[][]={{0.0,1.0,2.0,3.0},{4.0,5.0,6.0,7.0},{8.0,9.0,10.0,11.0},{12.0,13.0,14.0,15.0}}; 
		Matrix4D toto=new Matrix4D(init);  
		//toto=ID; 
		
		System.out.println(toto);
		System.out.println(toto.determinant()); 
		
	}
}
